package com.fbt.energy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.energy.entity.AO.SpPrAO;
import com.fbt.energy.entity.Ammeter;
import com.fbt.energy.entity.SpEquipment;
import com.fbt.energy.entity.SpProductionLine;
import com.fbt.energy.entity.SparePart;
import com.fbt.energy.entity.VO.PrVO;
import com.fbt.energy.service.*;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 电表设备表(Ammeter)表控制层
 *
 * @author makejava
 * @since 2021-12-12 19:05:41
 */
@CrossOrigin
@RestController
@Api(tags = {"电表设备表(Ammeter)表控制层"})
@RequestMapping("/ammeter")
public class AmmeterController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AmmeterService ammeterService;

    @Resource
    private SparePartService sparePartService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private SpEquipmentService spEquipmentService;

    @Autowired
    private OssService ossService;

    @Autowired
    private SpProductionLineService spPrService;

    /**
     * 新增数据
     *
     * @param ammeter 实体
     * @return 新增结果
     */
    @Log(title = "电表设备表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Ammeter> add(Ammeter ammeter) {

        return ResponseEntity.ok(this.ammeterService.insert(ammeter));
    }

    /**
     * 根据月份查询每天数据
     */
    @GetMapping("/getDayElectricity")
    @ApiOperation("根据月份查询每天数据")
    public List<PrVO> getDayElectricity(String queerTime) {
        return ammeterService.getDayElectricity(queerTime);
    }

    /**
     * 根据天数查询每小时数据
     */
    @GetMapping("/getAllMWH")
    @ApiOperation("根据天数查询每小时数据")
    public List<PrVO> getAllMWH(String queerTime) {

        return ammeterService.getAllMWH(queerTime);
    }

    /**
     * 查询每年的数据
     */
    @GetMapping("/getYearElectricity")
    @ApiOperation("查询每年的数据")
    public ResponseEntity getYearElectricity(String queerTime) {

        List<PrVO> yearElectricity = ammeterService.getYearElectricity(queerTime);

        return ResponseEntity.ok(yearElectricity);

    }





    /*-------------------------------备件清单------------------------------*/


    /**
     * 根据产线编号查询备件
     */
    @GetMapping("/getSparePartByPrNo")
    @ApiOperation("根据产线编号查询设备")
    public SpPrAO getSparePartByPrNo(String PrNo) {

        System.out.println("产线编号" + PrNo);
        SpPrAO spPrAO = spPrService.getSparePartByPrNo(PrNo);
        return spPrAO;
    }

    /**
     * 新增 产线
     */
    @ApiOperation("新增 产线")
    @PostMapping("/prAdd")
    public ResponseEntity<SpProductionLine> add(SpProductionLine spProductionLine) {
        return ResponseEntity.ok(this.spPrService.insert(spProductionLine));
    }


    /**
     * 修改产线
     */
    @Log(title = "备件_产线表", businessType = BusinessType.UPDATE)
    @ApiOperation("修改产线")
    @PutMapping("/prSet")
    public ResponseEntity<SpProductionLine> edit(SpProductionLine spProductionLine) {
        return ResponseEntity.ok(this.spPrService.update(spProductionLine));
    }


    /**
     * 删除产线
     */
    @Log(title = "备件_产线表", businessType = BusinessType.DELETE)
    @ApiOperation("删除产线")
    @DeleteMapping("/prDelete")
    public ResponseEntity<Boolean> deletePrById(String prNo) {
        return ResponseEntity.ok(this.spPrService.deleteById(prNo));
    }


    /**
     * 查询所有产线
     */
    @GetMapping("/getAllSparePart")
    @ApiOperation("查询所有产线")
    public List<SpPrAO> getAllSparePart() {
        List<SpPrAO> spPrAOList = spPrService.getAllSparePart();
        return spPrAOList;
    }


    /**
     * 新增  设备
     */
    @Log(title = "备件_设备表", businessType = BusinessType.INSERT)
    @ApiOperation("新增 设备")
    @PostMapping("/eqAdd")
    public ResponseEntity<SpEquipment> add(SpEquipment spEquipment) {
        return ResponseEntity.ok(this.spEquipmentService.insert(spEquipment));
    }


    /**
     * 修改 设备
     */
    @Log(title = "备件_设备表", businessType = BusinessType.UPDATE)
    @ApiOperation("修改设备")
    @PutMapping("/eqSet")
    public ResponseEntity<SpEquipment> edit(SpEquipment spEquipment) {
        return ResponseEntity.ok(this.spEquipmentService.update(spEquipment));
    }


    /**
     * 删除 设备
     */
    @Log(title = "备件_产线表", businessType = BusinessType.DELETE)
    @ApiOperation("删除 设备")
    @DeleteMapping("/eqDelete")
    public ResponseEntity<Boolean> deleteEqById(String eqNo) {
        return ResponseEntity.ok(this.spEquipmentService.deleteById(eqNo));
    }

    /**
     * 查询产线下所有设备
     */
    @GetMapping("/getAllEqByPrNo")
    @ApiOperation("查询产线下所有设备*")
    public List<SpEquipment> getAllEqByPrNo(String prNo) {
        List<SpEquipment> spEquipmentList = spEquipmentService.getAllEqByPrNo(prNo);

        return spEquipmentList;
    }


    /**
     * 新增 零件
     */
    @Log(title = "备件_清单表", businessType = BusinessType.INSERT)
    @ApiOperation("新增 零件")
    @PostMapping("/spAdd")
    public ResponseEntity<SparePart> add(SparePart sparePart) {
        return ResponseEntity.ok(this.sparePartService.insert(sparePart));
    }


    /**
     * 修改 零件
     */
    @Log(title = "备件_清单表", businessType = BusinessType.UPDATE)
    @ApiOperation("修改数据")
    @PutMapping("/SpSet")
    public ResponseEntity<SparePart> edit(SparePart sparePart) {
        return ResponseEntity.ok(this.sparePartService.update(sparePart));
    }


    /**
     * 删除 零件
     */
    @Log(title = "备件_清单表", businessType = BusinessType.DELETE)
    @ApiOperation("根据主键删除数据")
    @DeleteMapping("/spDelete")
    public ResponseEntity<Boolean> deleteSpById(String spNo) {
        return ResponseEntity.ok(this.sparePartService.deleteById(spNo));
    }

    /**
     * 查询设备下所有零件
     */
    @GetMapping("/getSparePartList")
    public List<SparePart> getSppareByEqNo(String eqNo) {
        List<SparePart> sparePartList = sparePartService.getsparePars(eqNo);
        return sparePartList;
    }


    /**
     * 根据备件名称模糊查询
     */
    @ApiOperation("根据备件名称模糊查询")
    @GetMapping("/likeSpareBySpName")
    public List<SparePart> likeSpareBySpName(String sparePartName) {

        QueryWrapper<SparePart> spareWrapper = new QueryWrapper<>();
        spareWrapper.like("spare_part_name", sparePartName);

        List<SparePart> spareParts = sparePartService.list(spareWrapper);
        return spareParts;
    }

    /**
     * 上传图片
     */
    @PostMapping("/oss")
    @ApiOperation("上传图片")
    public String uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        //返回上传到oss的路径
        String url = ossService.uploadFileAvatar(file);
        System.out.println("图片的地址" + url);

        return url;
    }

    /**向数据库中添加图片地址*/

    @PostMapping("/ossAdd")
    public SparePart ossAdd(SparePart sparePart){
       SparePart spareOss= sparePartService.ossAdd(sparePart);
        return spareOss;
    }

    /**删除数据库中图片地址*/
    @DeleteMapping("/deletePicture")
    public boolean deletePicture(String ext1){
        boolean  i =sparePartService.deletePicture(ext1);

        return i;
    }



}

